/* Copyright (c) 2006 by M Aamir Maniar 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */


namespace Generic.DataStructures
{
	using System;
	using System.Collections.Generic;
	using System.Text;
	using Generic.Utils;

	public class StackWithEvents<T> : IStack<T>
	{
		IStack<T> _internalStack;

		public StackWithEvents(IStack<T> stack)
		{
			ArgumentValidator.CheckIfNull<IStack<T>>(stack);
			_internalStack = stack;
		}

		#region IStack<T> Members

		public void Push(T item)
		{
            OnBeforePush(new StackEventArgs<T>(item));
			_internalStack.Push(item);
            OnAfterPush(new StackEventArgs<T>(item));
		}

        public event EventHandler<StackEventArgs<T>> BeforePush = null;
        protected virtual void OnBeforePush(StackEventArgs<T> e)
        {
            if (BeforePush != null)
                BeforePush(this, e);
        }

        public event EventHandler<StackEventArgs<T>> AfterPush = null;
        protected virtual void OnAfterPush(StackEventArgs<T> e)
        {
            if (AfterPush != null)
                AfterPush(this, e);
        }

		public T Pop()
		{
            OnBeforePop(new EventArgs());
			T item = _internalStack.Pop();
            OnAfterPop(new EventArgs());
            return item;
		}

        public event EventHandler BeforePop = null;
        protected virtual void OnBeforePop(EventArgs e)
        {
            if (BeforePop != null)
                BeforePop(this, e);
        }

        public event EventHandler AfterPop = null;
        protected virtual void OnAfterPop(EventArgs e)
        {
            if (AfterPop != null)
                AfterPop(this, e);
        }

		public T Top
		{
			get 
			{
				return _internalStack.Top;
			}
		}

		public bool FixedSize
		{
			get
			{
				return _internalStack.FixedSize;
			}
			set
			{
				_internalStack.FixedSize = value;
			}
		}

		public bool IsFull
		{
			get 
			{
				return _internalStack.IsFull;
			}
		}

		public bool IsEmpty
		{
			get 
			{
				return _internalStack.IsEmpty;
			}
		}

		public bool IsSynchronized
		{
			get 
			{
				return _internalStack.IsSynchronized;
			}
		}

		public int Capacity
		{
			get
			{
				return _internalStack.Capacity;
			}
			set
			{
				_internalStack.Capacity = value;
			}
		}

		public StackGrowingMode GrowMode
		{
			get
			{
				return _internalStack.GrowMode;
			}
			set
			{
				_internalStack.GrowMode = value;
			}
		}

		public object SyncRoot
		{
			get 
			{
				return _internalStack.SyncRoot;
			}
		}

		#endregion

		#region ICollection<T> Members

		public void Add(T item)
		{
			_internalStack.Add(item);
		}

		public void Clear()
		{
            OnBeforeClear(new EventArgs());
			_internalStack.Clear();
            OnAfterClear(new EventArgs());
		}

        public event EventHandler BeforeClear = null;
        protected virtual void OnBeforeClear(EventArgs e)
        {
            if (BeforeClear != null)
                BeforeClear(this, e);
        }

        public event EventHandler AfterClear = null;
        protected virtual void OnAfterClear(EventArgs e)
        {
            if (AfterClear != null)
                AfterClear(this, e);
        }

		public bool Contains(T item)
		{
			return _internalStack.Contains(item);
		}

		public void CopyTo(T[] array, int arrayIndex)
		{
			_internalStack.CopyTo(array, arrayIndex);
		}

		public int Count
		{
			get 
			{
				return _internalStack.Count;
			}
		}

		public bool IsReadOnly
		{
			get 
			{
				return _internalStack.IsReadOnly;
			}
		}

		public bool Remove(T item)
		{
			return _internalStack.Remove(item);
		}

		#endregion

		#region IEnumerable<T> Members

		public IEnumerator<T> GetEnumerator()
		{
			return _internalStack.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return ((System.Collections.IEnumerable)_internalStack).GetEnumerator();
		}

		#endregion

		#region ICloneable Members

		public object Clone()
		{
			return _internalStack.Clone();
		}

		#endregion
	}

	#region StackEvent Delegates
	//public delegate void StackPushedHanler<T>
	#endregion

	#region public class StackPushedEventArgs<T> : EventArgs
	public class StackPushedEventArgs<T> : EventArgs
	{
		#region public StackPushedEventArgs(T pushedItem)
		public StackPushedEventArgs(T pushedItem)
		{
			_pushedItem = pushedItem;
		}
		#endregion

		#region public T PushedItem
		private T _pushedItem;
		public T PushedItem
		{
			get { return _pushedItem; }
			set { _pushedItem = value; }
		}
		#endregion

	}
	#endregion

}
